////
//  TSTaskTableView.m
//  chart
//
//  Created by 曾斌 on 16/3/24.
//  Copyright (c) 2016年 xu. All rights reserved.
//

#import "TSUserTableView.h"

#import "TSRecieveMsgTableView.h"
#import "TSMessageListCell.h"
#import "TSRecieverListModel.h"
#import "TSMsgEditTableViewController.h"
#import "TSHomeViewController.h"
#import "TSTaskListModel.h"
#import "TSUserCell.h"
#import "TSTaskIngDetailController.h"
#import "TSAuditTaskDetailController.h"
#import "TSUserListModel.h"
@implementation TSUserTableView{
    NSArray<TSUserModel> *userList;
    TSUserListModel * userListModel;
}
@synthesize requestUrl,extData;
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        self.dataSource = self;
        self.delegate = self;
    }
    userListModel =[[TSUserListModel alloc] init];
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cell_identify = @"cellIdentify";
    TSUserCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
    if (!cell) {
        cell = [[TSUserCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
    }
    if(userList){
        [cell setModel:[userList objectAtIndex:indexPath.row]];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(userList){
        return [userList count];
    }
    return 0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - requestData
- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    userListModel.requestParams = [self createRequestParam];
    [userListModel fetchWithPath:self.requestUrl completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (userListModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            strongSelf->userList = userListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

-(NSMutableDictionary *)createRequestParam{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:10];
    [dic setObject:@(self.pageIndex) forKey:TSDictionaryKeyDefine_PageIndex];
    [dic setObject:@(self.pageSize) forKey:TSDictionaryKeyDefine_PageSize];
    if(extData){
        [dic addEntriesFromDictionary:extData];
    }
    return dic;
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    userListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [userListModel fetchWithPath:self.requestUrl completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (userListModel.list.count == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            strongSelf->userList = userListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}



@end


